package com.example.wangzh.developkit.utils;


import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by USER on 2017/3/9.
 */

public class TransformUtils {

    /**
     * 去除尾部的".0"
     *
     * @param doub
     * @return
     */
    public static String transformDouble(double doub) {
        doub = new BigDecimal(doub).setScale(2, RoundingMode.HALF_UP).doubleValue();
        String str = "" + doub;
        if (str.endsWith(".0")) {
            str = str.substring(0, str.indexOf(".0"));
        }
        return str;
    }

    /**
     * 去除尾部的".0"
     *
     * @param doub
     * @return
     */
    public static String getDecimalTwo(double doub) {
        doub = new BigDecimal(doub).setScale(2, RoundingMode.HALF_UP).doubleValue();
        String str = "" + doub;
        if (str.endsWith(".0")) {
            str = str.substring(0, str.indexOf(".0"));
        }
        return str;
    }

    public static int parseInt(String str) {
        int i = 0;
        try {
            i = Integer.parseInt(str);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return i;
    }

    public static double parseDouble(String str) {
        double d = 0;
        try {
            d = Double.parseDouble(str);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return d;
    }

    public static String getRemainTimeStrFromEnd(long endSeconds) {
        long remainSeconds = endSeconds - new Date().getTime() / 1000;
        long minutes = remainSeconds / 60 % 60;
        long hours = remainSeconds / 3600 % 24;
        long days = remainSeconds / 3600 / 24;
        StringBuffer sb = new StringBuffer("");
        if (days > 0) {
            sb.append(days);
            sb.append("天");
        }
        if (hours > 0) {
            sb.append(hours);
            sb.append("小时");
        }
        sb.append(minutes);
        sb.append("分");
        return sb.toString();
    }

    public static String getRemainTimeStr(long seconds) {
        long remainSeconds = seconds;
        long minutes = remainSeconds / 60 % 60;
        long hours = remainSeconds / 3600 % 24;
        long days = remainSeconds / 3600 / 24;
        StringBuffer sb = new StringBuffer("");
        if (days > 0) {
            sb.append(days);
            sb.append("天");
        }
        if (hours > 0) {
            sb.append(hours);
            sb.append("小时");
        }
        sb.append(minutes);
        sb.append("分");
        return sb.toString();
    }

    /**
     * @param time
     * @return "yyyy/MM/dd  E  HH:mm前"
     */
    public static String getFormatTime(long time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd  E  HH:mm:ss前");
        Date date = new Date(time);
        return format.format(date);
    }

    public static long getTimeFromFormat(String timeStr) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm");
        Date date = format.parse(timeStr);
        return date.getTime();
    }


    /**
     * 以友好的方式显示时间
     *
     * @return
     */
    public static String friendlyTime(long timeMillis) {
        String ftime = "";
        Date date = new Date(timeMillis);
        Calendar cal = Calendar.getInstance();

        //判断是否是同一天
        String curDate = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
        String paramDate = new SimpleDateFormat("yyyy-MM-dd").format(date);
        if (curDate.equals(paramDate)) {
            int hour = (int) ((cal.getTimeInMillis() - date.getTime()) / 3600000);
            if (hour == 0)
                ftime = Math.max((cal.getTimeInMillis() - date.getTime()) / 60000, 1) + "分钟前";
            else
                ftime = hour + "小时前";
            return ftime;
        }

        long lt = date.getTime() / 86400000;
        long ct = cal.getTimeInMillis() / 86400000;
        int days = (int) (ct - lt);
        if (days == 0) {
            int hour = (int) ((cal.getTimeInMillis() - date.getTime()) / 3600000);
            if (hour == 0)
                ftime = Math.max((cal.getTimeInMillis() - date.getTime()) / 60000, 1) + "分钟前";
            else
                ftime = hour + "小时前";
        } else if (days == 1) {
            ftime = "昨天";
        } else if (days == 2) {
            ftime = "前天";
        } else if (days > 2 && days <= 10) {
            ftime = days + "天前";
        } else if (days > 10) {
            ftime = new SimpleDateFormat("yyyy-MM-dd").format(date);
        }
        return ftime;
    }

    public static String getIntArrayDesc(int[] ints) {
        StringBuffer sb = new StringBuffer();
        sb.append("[");
        for (int i : ints) {
            sb.append(i).append(",");
        }
        if (ints.length > 0) {
            sb.deleteCharAt(sb.length() - 1);
        }
        sb.append("]");
        return sb.toString();
    }

    public static String md5(String string) {
        byte[] hash;
        try {
            hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("Huh, MD5 should be supported?", e);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("Huh, UTF-8 should be supported?", e);
        }
        StringBuilder hex = new StringBuilder(hash.length * 2);
        for (byte b : hash) {
            if ((b & 0xFF) < 0x10)
                hex.append("0");
            hex.append(Integer.toHexString(b & 0xFF));
        }
        return hex.toString( ).toLowerCase( );
    }

    // md5(token+'分隔符(-)'+当前时间+'分隔符(-)'+加密密钥)
    public static String sign(String token, long timeSeconds) {
        return md5(token + "-" + timeSeconds + "-" + "qz-kfw-app");
    }
}
